﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;

using ECCIT.Data;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.Mobile.Model;
using OgilvyOne.UKNow.Mobile.Model.Events;
using ECCIT.Extension.Weibo_v2.Common;
using OgilvyOne.UKNow.Mobile.Utilities;
using OgilvyOne.UKNow.DALFactory;
using OgilvyOne.UKNow.IDAL;

namespace OgilvyOne.UKNow.Mobile.APIs.Events
{
    public class GetUps : IHttpHandler
    {
        public bool IsReusable { get { return true; } }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string uid = context.Request.Params["uid"];

                string currindex = "0";
                if (context.Request.Params["current_page"] != null)
                {
                    if (context.Request.Params["current_page"] != "0")
                    {
                        currindex = (int.Parse(context.Request.Params["current_page"].ToString()) - 1).ToString();
                    }
                }

                string pagesize = "10";
                if (context.Request.Params["page_size"] != null)
                {
                    pagesize = context.Request.Params["page_size"];
                }

                if (uid != null)
                {
                    getALLMod(int.Parse(pagesize), int.Parse(currindex), context, int.Parse(uid));
                }
                else
                {
                    getALLMod(int.Parse(pagesize), int.Parse(currindex), context, null);
                }
            }
            catch (Exception)
            {
                context.Response.Write(ResultUtility.ReturnResultJson("-400", "", "获取列表失败"));
            }
        }

        private void getALLMod(int pagesize, int currindex, HttpContext htc, int? uid)
        {
            IDALEvents use = EventsDALFactory.Create(2052);

            PagingDataSet ds = use.getUpEventByUserID(2052, uid, currindex,pagesize);
            DataTable dt = ds.DataSet.Tables[0];

            ModelList<EventInfo> events = null;
            if (dt != null)
            {
                events = new ModelList<EventInfo>(pagesize, currindex, ds.Capacity);
                List<EventInfo> list = new List<EventInfo>();
                foreach (DataRow dr in dt.Rows)
                {
                    EventsModel model = use.GetEventsModelByID((int)dr["col_events_id"]);
                    if (model == null) continue;
                    list.Add(ModelConvertUtility.ToEventInfo(model, false));
                }
                events.List = list;
                //events.CurrentPageIndex = currindex + 1;
                string s = JsonSerializationHelper.ObjectToJson(events);
                htc.Response.Write(ResultUtility.ReturnResultJson("0", s, "获取列表成功"));
            }
        }
    }
}
